<template>
  <div>
    <yt-crud
      ref="crudRef"
      :data="data"
      :column="column"
      v-model:page="state.page"
      v-model:query="state.query"
      :total="state.total"
      :loading="state.loading"
      :tableProps=" {
        selection: false,
        viewBtn: true,
        delBtn: false,
        editBtn: false,
        menuSlot: true,
        menuWidth: 180,

      }"
      :fun-props="{
        exportBtn: false,
        delBtn: false,
        addBtn: false,
      }"
      @openBeforeFun="openBeforeFun"
      @onLoad="getData"
      @saveFun="onSave"
    >
      <template #type="scope:any">
        <DictTag :options="ProductTypeEnum" :value="scope.row.type" />
      </template>
      <template #status="scope:any">
        <el-switch v-model="scope.row.status" active-value="Enabled" inactive-value="Disabled" @change="handleStatusChange(scope.row)" />
      </template>
    </yt-crud>
  </div>
</template>

<script lang="ts" setup>

import YtCrud from '@/components/common/yt-crud.vue'
import { productsList, productSave, changeStatus } from '@/api/platform/product'

import {useI18n} from 'vue-i18n'
import {IColumn} from '@/components/common/types/tableCommon'
import {ProductsVo} from '@/api/platform/product/types'
import {ComponentInternalInstance} from 'vue'

const { proxy } = getCurrentInstance() as ComponentInternalInstance
const { EnableStatusEnum, ProductTypeEnum } = toRefs<any>(proxy?.useEnum('enable_status_enum', 'product_type_enum'))

const { t } = useI18n()

const crudRef = ref()

const data = ref<ProductsVo[]>([])
const column = ref<IColumn[]>([
  {
    label: t('platform.product.name'),
    key: 'name',
    search: true,
    rules: [{ required: true, message: t('platform.product.validator.name')}],
  },
  {
    label: t('platform.product.type'),
    key: 'type',
    type: 'select',
    componentProps: {
      options: ProductTypeEnum,
    },
    search: true,
    formHide: false,
    sortable: false,
    slot: true,
    rules: [{ required: true, message: t('platform.product.validator.type') }],
  },
  {
    label: t('platform.product.manufacturer'),
    key: 'manufacturer',
    rules: [{ required: true, message: t('platform.product.validator.manufacturer') }],
  },
  {
    label: t('platform.product.keepAliveTime'),
    key: 'keepAliveTime',
    type: 'number',
    search: false,
    rules: [{ required: true, message: t('platform.product.validator.keepAliveTime') }],
  },
  {
    label: t('platform.product.status'),
    key: 'status',
    type: 'select',
    componentProps: {
      options: EnableStatusEnum,
    },
    search: true,
    formHide: false,
    sortable: false,
    slot: true,
    rules: [{ required: true, message: t('platform.product.validator.status') }],
  },
  {
    label: t('crud.table.column.createTime'),
    key: 'createTime',
    tableWidth: 180,
    sortable: true,
    type: 'date',
    formHide: true,
  },
])

const state = reactive({
  total: 0,
  page: {
    pageSize: 12,
    pageNum: 1,
  },
  query: {},
  loading: false,
})

const getData = () => {
  state.loading = true
  productsList({
    ...state.page,
    ...state.query,
  })
  .then((res) => {
    data.value = res.data.rows || []
    state.total = res.data.total
  })
  .finally(() => {
    state.loading = false
  })
}
// 获取字典
const getDict = () => {

}


// 上传前置操作
const openBeforeFun = ({ type }) => {
  if (type === 'add') { /* empty */ }
}
getDict()
// 保存数据
const onSave = ({ type, data, cancel }: any) => {
  state.loading = true
  productSave(toRaw(data))
    .then(() => {
      ElMessage.success(t('operate.tooltip.success'))
      cancel()
      getData()
    })
    .finally(() => {
      state.loading = false
    })
}

/** 状态修改  */
const handleStatusChange = async (row: any) => {
  if(!row.id)  return
  try {
    await proxy?.$modal.confirm(row.status === 'Enabled' ? t('crud.action.tooltip.enable') : t('crud.action.tooltip.disabled'))
    await changeStatus(row.id, row.status)
    ElMessage.success(t('operate.tooltip.success'))
    getData()
  } catch (err) {
    row.status = row.status === 'Enabled' ? 'Disabled' : 'Enabled'
  }
}
</script>

<style lang="scss" scoped></style>
